package com.generator.statics.outboundStatics.service.impl;

import com.generator.statics.outboundStatics.dao.OutCountMapper;
import com.generator.statics.outboundStatics.pojo.*;
import com.generator.statics.outboundStatics.service.IOutCountService;
import com.generator.util.PageModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class OutCountService implements IOutCountService {

    @Autowired
    private OutCountMapper outCountMapper;

    public OutCountMapper getOutCountMapper() {
        return outCountMapper;
    }

    public void setOutCountMapper(OutCountMapper outCountMapper) {
        this.outCountMapper = outCountMapper;
    }

    //获取所有出库信息（去重）
    public List<OutCountObject> OutCountObjectHashMap(OutCountObject warehouse){
        List<OutCountObject> warehouseList = outCountMapper.getWarehouseList(warehouse);
        for (OutCountObject outCountObject : warehouseList) {
            float totalprice=0;
            OutCountDetail flag = new OutCountDetail();
            flag.setHousename(outCountObject.getHousename());
            List<OutCountDetail> outCountDetail = outCountMapper.getOutCountDetial(flag);
            HashMap<String, OutCountDetail> stringOutCountDetailHashMap = new HashMap<String, OutCountDetail>();
            Set<String> orderids =null;
            //创建基于查出来的所有的订单去重后的hashmap
            for (OutCountDetail countDetail : outCountDetail) {
                orderids = stringOutCountDetailHashMap.keySet();
                if(!orderids.contains(countDetail.getOrderid())){
                    stringOutCountDetailHashMap.put(countDetail.getOrderid(),countDetail);
                }else{
                    OutCountDetail outCountDetail1 = stringOutCountDetailHashMap.get(countDetail.getOrderid());
                    countDetail.setGoodscomment(countDetail.getGoodscomment()+outCountDetail1.getGoodscomment());
                    countDetail.setTotalprice(countDetail.getTotalprice()+outCountDetail1.getTotalprice());

                    stringOutCountDetailHashMap.put(countDetail.getOrderid(), outCountDetail1);
                }
            }

            Iterator iterator = null;
            if (orderids != null) {
                iterator = orderids.iterator();
                while(iterator.hasNext()){
                    String key = (String)iterator.next();
                    OutCountDetail outCountDetail1 = stringOutCountDetailHashMap.get(key);
                    totalprice+=outCountDetail1.getTotalprice();
                }
            }


            outCountObject.setOutcountnumber(stringOutCountDetailHashMap.size());
            outCountObject.setOutcountprice(totalprice);
        }

        //如果仓库的出库条目为0 就从出库信息中删除
        List<OutCountObject> warehouseList2= new ArrayList<OutCountObject>();
        for (OutCountObject outCountObject : warehouseList) {
            if(outCountObject.getOutcountnumber()!=0){
                warehouseList2.add(outCountObject);
            }
        }
        return warehouseList2;
    }

    @Override
    public PageModel<OutCountObject> getTotalOutCount(OutCountObject warehouse, PageModel<OutCountObject> pageModel) {

        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<OutCountObject> outCountObjects = OutCountObjectHashMap(warehouse);
        PageInfo<OutCountObject> warehousePageInfo = new PageInfo<OutCountObject>(outCountObjects);

        pageModel.setData(new ArrayList<OutCountObject>(outCountObjects));
        pageModel.setTotalPage(warehousePageInfo.getPages());
        pageModel.setTotalRows(warehousePageInfo.getTotal());
        return pageModel;
    }

    /**
     * 获取指定仓库的订单（去重合并）
     * @param warehouse
     * @return
     */
    public List<OutCountDetail> OutCountDetailHashMap(OutCountDetail warehouse){
        List<OutCountDetail> warehouseList = outCountMapper.getOutCountDetial(warehouse);

        HashMap<String, OutCountDetail> stringOutCountDetailHashMap = new HashMap<String, OutCountDetail>();
        Set<String> orderids =null;

        //创建基于查出来的所有的订单去重后的hashmap
        for (OutCountDetail countDetail : warehouseList) {
            orderids = stringOutCountDetailHashMap.keySet();
            if(!orderids.contains(countDetail.getOrderid())){
                stringOutCountDetailHashMap.put(countDetail.getOrderid(),countDetail);
            }else{
                Integer totalcomment;
                Float prototal;
                OutCountDetail outCountDetail1 = stringOutCountDetailHashMap.get(countDetail.getOrderid());
                totalcomment=countDetail.getGoodscomment()+outCountDetail1.getGoodscomment();
                prototal=countDetail.getTotalprice()+outCountDetail1.getTotalprice();
                outCountDetail1.setTotalprice(prototal);
                outCountDetail1.setGoodscomment(totalcomment);
                stringOutCountDetailHashMap.put(countDetail.getOrderid(), outCountDetail1);
            }
        }

        warehouseList.clear();
        Iterator iterator = null;
        if (orderids != null) {
            iterator = orderids.iterator();
            while(iterator.hasNext()){
                String key = (String)iterator.next();
                OutCountDetail outCountDetail1 = stringOutCountDetailHashMap.get(key);
                warehouseList.add(outCountDetail1);
            }
        }

        return warehouseList;
    }

    /**
     * 输出最终的仓库的订单
     * @param warehouse
     * @param pageModel
     * @return
     */
    @Override
    public PageModel<OutCountDetail> getOutCountDetail(OutCountDetail warehouse, PageModel<OutCountDetail> pageModel) {

        pageModel.setPageNum(pageModel.getPageNum()<=0?1:pageModel.getPageNum());
        pageModel.setPageSize(pageModel.getPageSize()<=0?3:pageModel.getPageSize());
        PageHelper.startPage(pageModel.getPageNum(), pageModel.getPageSize());
        List<OutCountDetail> warehouseList = OutCountDetailHashMap(warehouse);

        PageInfo<OutCountDetail> warehousePageInfo = new PageInfo<OutCountDetail>(warehouseList);


        pageModel.setData(new ArrayList<OutCountDetail>(warehouseList));
        pageModel.setTotalPage(warehousePageInfo.getPages());
        pageModel.setTotalRows(warehousePageInfo.getTotal());
        return pageModel;
    }

    //取得订单详情（通过订单号）
    @Override
    public OutCountDelivery getDeliveryByOrderId(OutCountDelivery outCountDelivery) {
        List<OutCountDelivery> deliveryByOrderId = outCountMapper.getDeliveryByOrderId(outCountDelivery);

        float totalprice=0;
        for (OutCountDelivery countDelivery : deliveryByOrderId) {
            totalprice+=countDelivery.getTotalprice();
        }

        OutCountDelivery outCountDelivery1 = deliveryByOrderId.get(0);
        outCountDelivery1.setTotalprice(totalprice);
        Integer outCountZhuangTaiId = outCountDelivery1.getOutCountZhuangTaiId();
        Integer shengHeZhuangTaiId = outCountDelivery1.getShengHeZhuangTaiId();
        switch (outCountZhuangTaiId){
            case 1 :outCountDelivery1.setOutCountZhuangTai("未发货");break;
            case 2 :outCountDelivery1.setOutCountZhuangTai("已发货");break;
            case 3 :outCountDelivery1.setOutCountZhuangTai("已回款");break;
            case 4 :outCountDelivery1.setOutCountZhuangTai("取消订单");break;
            case 5 :outCountDelivery1.setOutCountZhuangTai("已退货");break;
            default:
                System.out.println("你什么问题？");
        }

        switch (shengHeZhuangTaiId){
            case 0:outCountDelivery1.setShengHeZhuangTai("未审核");break;
            case 1:outCountDelivery1.setShengHeZhuangTai("审核中");break;
            case 2:outCountDelivery1.setShengHeZhuangTai("审核通过");break;
            case 3:outCountDelivery1.setShengHeZhuangTai("审核不通过");break;
            default:
                System.out.println("你什么大病？");
        }
        return outCountDelivery1;
    }

    //确定指定订单号的商品
    @Override
    public List<CommentDetial> getCommentDetialByOrderId(CommentDetial commentDetial) {
        return outCountMapper.getCommentDetialByOrderId(commentDetial);
    }

    //取得所有省份
    @Override
    public List<Province> getAllProvince(Province province) {
        return outCountMapper.getAllProvince(province);
    }

    //取得所有城市
    @Override
    public List<City> getAllCity(City city) {
        return outCountMapper.getAllCity(city);
    }
}
